#include "EventWrapper.h"
#include "RowModel.h"
#include "AddBlockToRowNetworkEvent.h"
#include "UpdateScoreInternalEvent.h"

RowModel::RowModel(size_t width)
    : width(width)
{
}

RowModel::~RowModel()
{
}

bool RowModel::isFull()
{
    return blocks.size() == width;
}

void RowModel::addBlock(int playerId, const Position& position, const Color& color)
{
    updateScore(playerId);
    BlockModel* block = new BlockModel(Position(position.x, 0));
    blocks.push_back(block);
    addChild(block);
    addChangeEvent(new AddBlockToRowNetworkEvent(id, position, color));
}

bool RowModel::noExistingBlock(const Position& position)
{
    for (BlockModels::iterator it = blocks.begin(); it != blocks.end(); ++it)
    {
        if (position.x == (*it)->getPosition().x)
        {
            return false;
        }
    }
    return true;
}

BlockModels RowModel::getBlocks()
{
    return blocks;
}

void RowModel::updateScore(int playerId)
{
    if (blocks.empty())
    {
        EventWrapper::push(new UpdateScoreInternalEvent(playerId, UpdateScoreInternalEvent::NEW_ROW));
    }
}
